package com.example.distinguish_server.Utils.Html;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
 public class HttpUtil {
    public static String post(String url, String data) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
         // 设置请求体
        StringEntity requestEntity = new StringEntity(data, "UTF-8");
        httpPost.setEntity(requestEntity);
         // 设置请求头
        httpPost.setHeader("Content-Type", "application/json");
         // 发送请求并获取响应
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();
         // 解析响应
        String responseBody = EntityUtils.toString(responseEntity);
         return responseBody;
    }

     public static String post(String url, String accessToken, String param) throws IOException {
         HttpClient httpClient = HttpClientBuilder.create().build();
         HttpPost httpPost = new HttpPost(url);

         // 设置请求体
         StringEntity requestEntity = new StringEntity(param, "UTF-8");
         httpPost.setEntity(requestEntity);

         // 设置请求头
         httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded  ");
         httpPost.setHeader("access_token", accessToken);

         // 发送请求并获取响应
         HttpResponse response = httpClient.execute(httpPost);
         HttpEntity responseEntity = response.getEntity();

         // 解析响应
         String responseBody = EntityUtils.toString(responseEntity);

         return responseBody;
     }
}